﻿/******************************************************************************************************
Copyright (c) 2014 Steven Giacomelli (stevegiacomelli@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this
software and associated documentation files (the "Software"), to deal in the Software
without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************************************/

using System;
using System.Collections.Generic;
using System.Linq;

namespace SharpGeo.IO.ShapeFiles
{
    /// <summary>
    /// A ShapeFileData representing a polyshape (polyline or polygon)
    /// </summary>
    public class ShapeFilePolyShape : ShapeFileData
    {
        #region Constructor

        /// <summary>
        /// Constructs a ShapeFilePolyShape
        /// </summary>
        /// <param name="numberOfParts">The number of parts</param>
        /// <param name="numberOfAxes">The number of axes</param>
        public ShapeFilePolyShape(int numberOfParts, int numberOfAxes)
        {
            _parts = new double[numberOfParts][];
            NumberOfAxes = numberOfAxes;
        }

        #endregion Constructor

        #region Fields

        private double[][] _parts = null;

        #endregion Fields

        #region Properties

        /// <summary>
        /// Gets the number of parts
        /// </summary>
        public int NumberOfParts { get { return _parts.Length; } }

        /// <summary>
        /// Gets the number of axes
        /// </summary>
        public int NumberOfAxes { get; protected set; }

        #endregion Properties

        #region Methods

        /// <summary>
        /// Sets the value of a part
        /// </summary>
        /// <param name="part">The coordinates of the part</param>
        /// <param name="index">The index to set</param>
        public void SetPart(double[] part, int index)
        {
            _parts[index] = part;
        }

        /// <summary>
        /// Gets a part
        /// </summary>
        /// <param name="index">The index to get</param>
        /// <returns>The part</returns>
        public double[] GetPart(int index)
        {
            return _parts[index];
        }

        /// <summary>
        /// Converts the ShapeFile PolyShapes to SharpGeo.PolyShape
        /// </summary>
        /// <param name="targetGeometry">The geometry to target the conversion to</param>
        /// <returns>The converted PolyShape</returns>
        public PolyShape[] ToPolyShapes(GeometryType targetGeometry)
        {
            var rings = new List<LinearRing>(_parts.Length);
            var polyshapes = new List<PolyShape>();

            for (int i = 0; i < _parts.Length; i++)
            {
                rings.Add(new LinearRing(_parts[i], NumberOfAxes));
            }

            if (targetGeometry == GeometryType.Polygon)
            {
                var exteriorRings = (from ring in rings where ring.IsClockwise() select ring).ToList();
                var interiorRings = (from ring in rings where !ring.IsClockwise() select ring).ToList();

                //[TODO] require logic that determine what interiorring is in what exterior ring
                //for now shortcut this

                //with more than one exterior ring ignore the interior rings
                if (exteriorRings.Count > 1)
                {
                    for (int i = 0; i < exteriorRings.Count; i++)
                    {
                        polyshapes.Add(new PolyShape(Guid.NewGuid(), exteriorRings[0], null, targetGeometry));
                    }
                }
                else if (exteriorRings.Count == 1)
                {
                    polyshapes.Add(new PolyShape(Guid.NewGuid(), exteriorRings[0], interiorRings.ToArray(), targetGeometry));
                }
                else
                {
                    return null;
                }
            }
            else if (targetGeometry == GeometryType.Polyline)
            {
                for (int i = 0; i < rings.Count; i++)
                {
                    polyshapes.Add(new PolyShape(Guid.NewGuid(), rings[0], null, targetGeometry));
                }
            }
            else
            {
                return null;
            }

            return polyshapes.ToArray();
        }

        #endregion Methods
    }
}